﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.BUS;
using ATW.MT.CommonClass;
using System.Data;
using Com.Alipay;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Text;
using System.Configuration;

namespace ATW.MT
{
    public partial class RequestDetail : System.Web.UI.Page
    {
        bus_T_Request request = new bus_T_Request();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                string id = Request.QueryString["RequestID"];
                if (!string.IsNullOrEmpty(id) && common.filterChar(id))
                {
                    DataTable dtHead = new DataTable();
                    DataTable dtList = new DataTable();
                    GetData(id, ref dtHead, ref dtList);
                    ViewState["dtList"] = dtList;
                    rpList.DataSource = dtHead;
                    rpList.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.Debug("RequestDetail", ex.Message.ToString());
            }
        }
        protected void lbnDetail_Click(object sender, EventArgs e)
        {//显示详情
            LinkButton lbn = (LinkButton)sender;
            LinkButton lbnHide = (LinkButton)lbn.Parent.FindControl("lbnHide");
            HiddenField hdf = (HiddenField)lbn.Parent.FindControl("hdfOid");
            Repeater rpSub = (Repeater)lbn.Parent.FindControl("rpSub");
            Control trDetail = (Control)lbn.Parent.FindControl("trDetail");
            try
            {
                lbn.Visible = false;
                lbnHide.Visible = true;
                trDetail.Visible = true;
                DataTable dt = (DataTable)ViewState["dtList"];
                DataView dv = dt.DefaultView;
                dv.RowFilter = "OID='" + hdf.Value + "'";
                dt = dv.ToTable();
                dt.Columns.Add("sum");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string num = dt.Rows[i]["Buy_num"].ToString();
                    string price = dt.Rows[i]["finla_price"].ToString();
                    int _num = 0;
                    decimal _price = 0;
                    if (int.TryParse(num, out _num) && decimal.TryParse(price, out _price))
                    {
                        dt.Rows[i]["sum"] = _num * _price;
                    }
                    else
                    {
                        dt.Rows[i]["sum"] = 0;
                    }
                }
                rpSub.DataSource = dt;
                rpSub.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnHide_Click(object sender, EventArgs e)
        {//隐藏
            LinkButton lbn = (LinkButton)sender;
            LinkButton lbnShow = (LinkButton)lbn.Parent.FindControl("lbnDetail");
            Control trDetail = (Control)lbn.Parent.FindControl("trDetail");
            try
            {
                lbn.Visible = false;
                lbnShow.Visible = true;
                trDetail.Visible = false;
            }
            catch (Exception ex)
            {

            }
        }
        private void GetData(string strRequestId, ref DataTable dtHead, ref DataTable dtList)
        {
            string strUrl = ConfigurationManager.AppSettings["UploadFilePath"];
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LanId", "zh");
            parm.Add("@RequestId", strRequestId);
            parm.Add("@strUrl", strUrl);
            //DataTable dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetailHead", parm);
            dtHead = request.getDataTable("SP_SelectRequestDetailHead", parm);

            Dictionary<string, object> parm1 = new Dictionary<string, object>();
            parm1.Add("@LanId", "zh");
            parm1.Add("@RequestId", strRequestId);
            parm1.Add("@strUrl", strUrl);
            //DataTable dtList = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetail", parm1);
            dtList = request.getDataTable("SP_SelectRequestDetail", parm1);
            dtList.Columns.Add("Oid");

            #region 匹配明细头、内容
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                for (int j = 0; j < dtHead.Rows.Count; j++)
                {
                    if (
                        dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                        dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                        dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                        dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                        dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                        dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                        dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                        dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                        dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                        dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                        dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                        dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                        dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                        dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                        dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                        dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                        dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                        dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                        dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                        dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                        dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                        dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                        dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                        dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                        dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                        dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                        dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                        dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                    {
                        dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                    }
                }

            }
            #endregion
        }
        public string subStr(string s, int maxLen)
        {
            if (s.Length >= maxLen)
            {
                return s.Substring(0, maxLen - 3) + "...";
            }
            return s;
        }
    }
}